/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.info;

import com.google.gwt.cell.client.Cell;
import finbudget.client.ui.table.Record;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.info.delete.DeleteColumn;
import finbudgettable.client.ui.grid.column.GridColumn;
import finbudgettable.client.ui.grid.event.RecordChangedEvent;
import finbudgettable.client.ui.grid.event.RecordChangedHandler;

public class InfoColumn extends GridColumn<Record> {

    public static final String FIELD_NAME = "@info";

    protected InfoColumn(Builder builder) {
        super(builder);
    }

    @Override
    protected boolean isValueChanged(Record record, Record value) {
        return false;
    }

    @Override
    protected String getCellStyleNames() {
        return InfoCell.RESOURCES.style().bInfoCell();
    }

    @Override
    public Record getValue(Record record) {
        return record;
    }

    @Override
    public boolean canBeHidden() {
        return false;
    }

    protected static class Builder extends GridColumn.Builder<InfoCell, Record> {

        Builder(CellGrid grid, String fieldName, boolean hasTotals) {
            super(grid, fieldName, hasTotals);
            setResizable(false);
            setDraggable(false);
            setColumnMenu(null);
            setSortable(false);
            setWidth("16px");
        }

        @Override
        protected InfoCell createCell() {
            return new InfoCell(grid);
        }

        @Override
        protected GridColumn<Record> createColumn(Cell<Record> cell) {
            return new InfoColumn(this);
        }

        @Override
        public GridColumn<Record> build() {
            GridColumn<Record> column = super.build();
            int colIndex = 0;
            if (grid.getColumnCount() > 0) {
                if (grid.getColumn(0) instanceof DeleteColumn) {
                    colIndex = 1;
                }
            }
            grid.moveColumn(column, colIndex);
            grid.addHandler(
                    new RecordChangedHandler() {
                        @Override
                        public void onRecordChanged(RecordChangedEvent event) {
                            grid.refreshCell(event.getRecord(), FIELD_NAME);
                        }
                    },
                    RecordChangedEvent.TYPE);
            return column;
        }
    }

    public static Builder builder(CellGrid grid, boolean hasTotals) {
        return new Builder(grid, FIELD_NAME, hasTotals);
    }

}
